namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System;
    using System.Xml;
    using System.Xml.Serialization;

    [XmlRoot("a")]
    public class Anchor : InnerElements, IAttributeAccesskey, IAttributeTabIndex, IXmlSerializable
    {
        public Anchor()
        {
        }

        public Anchor(string href, string rel)
            : this(null == href ? null as Uri : new Uri(href, UriKind.RelativeOrAbsolute), rel)
        {
        }

        public Anchor(Uri href, string rel)
        {
            this.Hyperlink = href;
            this.Relationship = rel;
        }

        public char? Accesskey
        {
            get;
            set;
        }

        public Uri Hyperlink
        {
            get;
            set;
        }

        public string Relationship
        {
            get;
            set;
        }

        public string ReverseLink
        {
            get;
            set;
        }

        public int? TabIndex
        {
            get;
            set;
        }

        public override IElement Deserialize(string name, string type, string xml)
        {
            IElement result = null;

            if (null == type && "a".Equals(name, StringComparison.OrdinalIgnoreCase))
            {
                result = xml.Deserialize<Anchor>();
            }

            return result;
        }

        public override void ReadXml(XmlReader reader)
        {
            string value = reader.GetAttribute("accesskey");
            if (!string.IsNullOrEmpty(value))
            {
                this.Accesskey = value[0];
            }

            value = reader.GetAttribute("href");
            if (!string.IsNullOrEmpty(value))
            {
                this.Hyperlink = new Uri(value, UriKind.RelativeOrAbsolute);
            }

            value = reader.GetAttribute("rel");
            if (!string.IsNullOrEmpty(value))
            {
                this.Relationship = value;
            }

            value = reader.GetAttribute("rev");
            if (!string.IsNullOrEmpty(value))
            {
                this.ReverseLink = value;
            }

            value = reader.GetAttribute("tabindex");
            if (!string.IsNullOrEmpty(value))
            {
                this.TabIndex = XmlConvert.ToInt32(value);
            }

            base.ReadXml(reader);
        }

        public override void WriteXml(XmlWriter writer)
        {
            if (this.Accesskey.HasValue)
            {
                writer.WriteAttributeString("accesskey", this.Accesskey.Value.ToString());
            }

            if (null != this.Hyperlink)
            {
                writer.WriteAttributeString("href", this.Hyperlink.IsAbsoluteUri ? this.Hyperlink.AbsoluteUri : this.Hyperlink.OriginalString);
            }

            if (!string.IsNullOrEmpty(this.Relationship))
            {
                writer.WriteAttributeString("rel", this.Relationship);
            }

            if (!string.IsNullOrEmpty(this.ReverseLink))
            {
                writer.WriteAttributeString("rev", this.ReverseLink);
            }

            if (this.TabIndex.HasValue)
            {
                writer.WriteAttributeString("tabindex", XmlConvert.ToString(this.TabIndex.Value));
            }

            base.WriteXml(writer);
        }
    }
}